import Vue from 'vue';
import VueRouter from 'vue-router';
import HomePage from '../views/HomePage';
import TypePage from '../views/TypePage';
import CartPage from '../views/CartPage';
import AboutPage from '../views/AboutPage';
import DetailPage from '../views//DetailPage';
import SearchPage from '../views/SearchPage';
import LoginPage from '../views/LoginPage';
// component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: "/home"
    },
    {
        path: "/home",
        name: "home",
        component: HomePage,
        meta:{
            TnavShow: true,
            BnavShow: true
        }
    },
    {
        path: "/types",
        name: "types",
        component: TypePage,
        meta:{
            TnavShow: true,
            BnavShow: true
        }
    },
    {
        path: "/cart",
        name: "cart",
        component:CartPage,
        meta:{
            TnavShow: false,
            BnavShow: false,
            private:true
        }
    },
    {
        path: "/about",
        name: "about",
        component:AboutPage,
        meta:{
            TnavShow: false,
            BnavShow: true,
            private:true
        }
    },
    {
        path: "/detail",
        name: "detail",
        component:DetailPage,
        meta:{
            TnavShow: false,
            BnavShow: false
        }
    },
    {
        path: "/search",
        name: "search",
        component:SearchPage,
        meta:{
            TnavShow: true,
            BnavShow: false,
            hideBottom:true,
            showBack:true
        }
    },
    {
        path: "/login",
        name: "login",
        component:LoginPage,
        meta:{
            TnavShow: false,
            BnavShow: false
        }
    }
]

const router = new VueRouter({
    mode:"hash",
    routes
})

// 防止出现重复跳转问题导致程序死循环
router.beforeEach(function(to,from,next){
    if(!to.meta.private){
        next();
        return;
    }

    let token = localStorage.getItem("token");
    if(token){
        next()
    }else{
        window.$showTip("请先登录");
        setTimeout(()=>{
            next({
                name:"login",
                params:{
                    url:to.fullPath
                }
            })
        },1000)
    }
})

export default router
